package com.peanut.baby.mvp.livedetail.group;

import android.app.Activity;

import com.peanut.baby.InitManager;
import com.peanut.baby.http.BaseObserver;
import com.peanut.baby.http.BaseResponse;
import com.peanut.baby.http.BaseScheduler;
import com.peanut.baby.http.RetrofitClient;
import com.peanut.baby.model.BBSComment;
import com.peanut.baby.model.LiveGroupRel;
import com.peanut.baby.model.LiveMsg;

import java.util.List;

import cn.nekocode.rxlifecycle.RxLifecycle;
import io.reactivex.Observable;

/**
 * Created by anonymouslystudio on 2017/8/22.
 */

public class LiveDetailGroupPresenter implements LiveDetailGroupContract.Presenter {
    private LiveDetailGroupContract.View view;
    Activity activity;

    public LiveDetailGroupPresenter(LiveDetailGroupContract.View view, Activity activity) {
        this.view = view;
        this.activity = activity;
    }

    @Override
    public void joinRooms(String roomIds,String points) {
        String token = InitManager.getInstance().getUser().userToken;
        String uid = InitManager.getInstance().getUserId();
        Observable observable = RetrofitClient.getInstance().enroleLiveRoom2(token,
                uid, roomIds,points);

        observable.compose(BaseScheduler.<BaseResponse<String>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    protected void onHandleSuccess(String o) {
                        view.onJoinSuccess(roomIds);
                    }

                    @Override
                    protected void onHandleError(String code, String msg) {
                        view.onRequestFailed(code, msg);
                    }

                });
    }

    @Override
    public void messageListGet(String roomId) {
        Observable observable = RetrofitClient.getInstance().getLiveRoomMessages(InitManager.getInstance().getUser().userToken,roomId,"");
        observable.compose(BaseScheduler.<BaseResponse<List<LiveMsg>>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<List<LiveMsg>>() {
                    @Override
                    protected void onHandleSuccess(List<LiveMsg> o) {
                        view.onMessageListGet(o);

                    }

                    @Override
                    protected void onHandleError(String code,String msg) {
                        view.onRequestFailed("100","获取消息列表失败: "+msg);
                    }

                });
    }
    @Override
    public void messageListGetByGroupId(String groupId) {
        Observable observable = RetrofitClient.getInstance().getLiveRoomMessagesByGroupId(InitManager.getInstance().getUser().userToken,groupId);
        observable.compose(BaseScheduler.<BaseResponse<List<LiveMsg>>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<List<LiveMsg>>() {
                    @Override
                    protected void onHandleSuccess(List<LiveMsg> o) {
                        view.onMessageListGet(o);

                    }

                    @Override
                    protected void onHandleError(String code,String msg) {
                        view.onRequestFailed("100","获取消息列表失败: "+msg);
                    }

                });
    }

    @Override
    public void getLiveRoomsByGroupId(String groupId) {
        Observable observable = RetrofitClient.getInstance().getLiveRoomsByGroupId(groupId);
        observable.compose(BaseScheduler.<BaseResponse<List<LiveMsg>>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<List<LiveGroupRel>>() {
                    @Override
                    protected void onHandleSuccess(List<LiveGroupRel> o) {
                        view.onLiveRoomsGet(o);

                    }

                    @Override
                    protected void onHandleError(String code,String msg) {
                        view.onRequestFailed("100","获取消息列表失败: "+msg);
                    }

                });
    }

}
